<template>
    <view>
        <uni-list>
            <uni-list-item
                v-for="(item, index) in pendingServiceItem"
                :key="index"
                @click="() => selectItem(item)"
                :clickable="true"
                :title="item.siName"
                :rightText="'剩余' + item.spSpqu + item.siUnit"
            >
            </uni-list-item>
            <u-empty v-if="pendingServiceItem.length === 0" text="暂无数据"> </u-empty>
        </uni-list>
    </view>
</template>

<script>
import { getCustomerPendingServiceItem, createCurrentServiceItem, getCustomerCurrentServiceItem } from "../../utils/api";
import { mapGetters } from "vuex";

export default {
    data() {
        return {
            pendingServiceItem: [],
            serviceRecord: {},
            customerRankFissionAmount: [], // 裂变订单总额
            crUuid: "",
            requestStatus: false,
        };
    },

    async onLoad(params) {
        this.crUuid = params.crUuid;
        let res = await getCustomerPendingServiceItem({ ciUuid: params.ciUuid });
        let res1 = await getCustomerCurrentServiceItem({ crUuid: params.crUuid });
        if (res && res1) {
            this.pendingServiceItem = res?.filter((item) => res1.findIndex((_item) => _item.siUuid === item.siUuid) === -1) || [];
        }
    },

    computed: {
        ...mapGetters({
            staffInfo: "personInfo/staffInfo",
        }),
    },

    methods: {
        async selectItem(item) {
            if (this.requestStatus) return false;
            this.requestStatus = true;
            if (item.spSpqu > 0) {
                let saveData = {
                    crUuid: this.crUuid,
                    siUuid: item.siUuid,
                    cdCrti: new Date().getUTCTime(),
                    cdCust: "待确认",
                    cdQuat: 1,
                    cdSsid: this.staffInfo.orUuid,
                    cdSsna: this.staffInfo.orName,
                    cdSsph: this.staffInfo.orPhon,
                    cdStst: "待确认",
                    siName: item.siName,
                };
                let res = await createCurrentServiceItem(saveData);
                if (res) {
                    let pages = getCurrentPages();
                    let beforePage = pages[pages.length - 2];
                    uni.navigateBack({
                        success: function () {
                            beforePage.onLoad({ ciUuid: item.ciUuid });
                        },
                    });
                }
            }
            setTimeout(() => {
                this.requestStatus = false;
            });
        },
    },
};
</script>

<style lang="scss" scoped></style>
